package cn.javacv.gulimall.product.service.impl;

import cn.javacv.gulimall.product.dao.CategoryDao;
import cn.javacv.gulimall.product.entity.CategoryEntity;
import cn.javacv.gulimall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.javacv.gulimall.common.utils.PageUtils;
import cn.javacv.gulimall.common.utils.Query;

import cn.javacv.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);

        List<CategoryEntity> level1= categoryEntityList
                .stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map((menu)->{
                    menu.setChild(getChildren(menu,categoryEntityList));
                    return menu;
                })
                .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());

        return level1;
    }

    @Override
    public Long[] findCategoryPath(long categoryId) {
        List<Long> paths = new ArrayList<>();
        findParentPath(categoryId,paths);
        Collections.reverse(paths);
        return paths.toArray(new Long[0]);
    }

    private void findParentPath(Long catId,List<Long> paths){
        // 收集当前节点id
        paths.add(catId);
        CategoryEntity  categoryEntity = this.getById(catId);
        if (categoryEntity.getParentCid() != 0){
            findParentPath(categoryEntity.getParentCid(),paths);
        }
    }

    @Override
    public void removeCategoryByIds(List<Long> asList) {
        //TODO 1.检查被删除的类目是不是被引用
        baseMapper.deleteBatchIds(asList);
    }

    @Transactional
    @Override
    public boolean updateDetail(CategoryEntity category) {
        this.updateById(category);
        if (StringUtils.isNotEmpty(category.getName())){
            categoryBrandRelationService.updateCategoryRelation(category.getCatId(),category.getName());
            //TODO 以后这里还可能添加其他的
        }
        return true;
    }

    private List<CategoryEntity> getChildren(CategoryEntity root , List<CategoryEntity> all){
        List<CategoryEntity> categoryEntityList = all
                .stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == root.getCatId())
                .map(categoryEntity -> {
                    categoryEntity.setChild(getChildren(categoryEntity,all));
                    return categoryEntity;})
                .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());
        return categoryEntityList;
    }



}
